package com.apes.fn.scm.prepare.model;

import com.apes.fn.scm.contract.FnContract;
import com.apes.fn.scm.contract.FnContractRepository;
import com.apes.framework.api.SpringManager;
import com.apes.framework.jpa.DefaultPersistListener;
import com.apes.framework.jpa.Model;
import com.apes.framework.jpa.comment.Comment;
import com.apes.framework.jpa.generator.ApesGenerator;
import com.apes.framework.jpa.spec.JpaDsl;
import com.apes.framework.util.ApesConst;
import com.apes.framework.util.JpaUtil;
import com.apes.framework.util.MapUtil;
import com.apes.scm.masterdata.enumeration.model.Enumeration;
import com.apes.scm.masterdata.frame.model.Dept;
import com.apes.scm.masterdata.frame.repository.DeptRepository;
import com.apes.scm.masterdata.party.model.PartyRole;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SelectBeforeUpdate;

import javax.persistence.*;
import java.util.*;

/**
 * 待缴款单
 * <p>
 * mut
 */
@Getter
@Setter
@SelectBeforeUpdate
@DynamicInsert
@DynamicUpdate
@Entity
@EntityListeners(DefaultPersistListener.class)
@Table(name = ToPrepare.CONST_TABLE_NAME)
public class ToPrepare extends Model {
    public static final String CONST_TABLE_NAME = "mas_to_prepare";

    @Id
    @Column(name = "id")
    @ApesGenerator(code = CONST_TABLE_NAME, name = "编号", padding = 5)
    @Comment("编码")
    private String id;  //编码

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "company_id", referencedColumnName = "id")
    @Comment("公司")
    private PartyRole company;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "dept_id", referencedColumnName = "id")
    @Comment("部门")
    private Dept dept;

    @Column(name = "collect_money_date", length = 3)
    @Comment("收款日期")
    private Date collectMoneyDate;

    @Column(name = "payment_money_date", length = 3)
    @Comment("缴款日期")
    private Date paymentMoneyDate;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "payment_type", referencedColumnName = "id")
    @Comment("收款类型")
    private Enumeration paymentType;

    @Column(name = "amount", nullable = false, columnDefinition = ApesConst.FIELD_QTY_16_4_0)
    @Comment("收款金额")
    private double amount = 0.0;

    @Column(name = "recovery_Amount", nullable = false, columnDefinition = ApesConst.FIELD_16_4_0)
    @Comment("已缴款金额")
    private double recoveryAmount = 0; // 已扣款金额

    @Column(name = "freeze_amount", nullable = false, columnDefinition = ApesConst.FIELD_16_4_0)
    @Comment("冻缴款金额")
    private double freezeAmount = 0;

    @OneToMany(mappedBy = "toPrepare", cascade = CascadeType.ALL, orphanRemoval = true)
    private Set<ToPrepareDetail> toPrepareDetails = new HashSet(); //付款单明细

    public Set<ToPrepareDetail> getToPrepareDetails() {
        if (toPrepareDetails == null || toPrepareDetails.isEmpty()) return new HashSet<>();
        for (ToPrepareDetail item : toPrepareDetails) {
            item.setToPrepare(this);
        }
        return toPrepareDetails;
    }

    @Column(name = "state")
    @Comment("状态")
    private String state;

    @Column(name = "note")
    @Comment("备注")
    private String note;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "fn_contract_id", referencedColumnName = "id")
    @Comment("赋能合同号")
    private FnContract fnContract;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "platform_company_id", referencedColumnName = "id")
    @Comment("平台公司")
    private PartyRole platformCompany;

    public void setFnContract() {
        List<FnContract> fnContracts = SpringManager.getBean(FnContractRepository.class).findAll(JpaDsl.toCriteriaByEq("partner.id", getCompany().getId(), "state", "approve"));
        if (fnContracts.size() < 0) return;
        this.setFnContract(fnContracts.get(0));
        this.setPlatformCompany(fnContracts.get(0).getCompany());
    }

    /**
     * 采购部门
     **/
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "purchase_dept_id", referencedColumnName = "id")
    @Transient
    private Dept purchaseDept;
}
